{ Statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=1015
  Verdict: Accepted
}
uses
	SysUtils, Math;
 
var   
	i,j:longint;
	n,m,ans:int64;
	a:array[-10..110,-10..110] of int64;
	b,w:array[-10..110,-10..110] of boolean;
 
function dfs(i,j:longint):boolean;
begin
result:=false;
w[i][j]:=true;
if (a[i-1][j]<a[i][j]) or (a[i][j-1]<a[i][j]) or (a[i][j+1]<a[i][j]) or (a[i+1][j]<a[i][j]) then begin
	result:=true;
	exit;
end;
if a[i-1][j] = a[i][j] then begin
	if w[i-1][j] and b[i-1][j] then begin
		result:=true;
		exit;
	end else
	if (not w[i-1][j]) and (i>1) then begin
		b[i-1][j]:=dfs(i-1,j);
		if b[i-1][j] then begin
			result:=true;
			exit;
		end;
	end;
end;
if a[i+1][j] = a[i][j] then begin
	if w[i+1][j] and b[i+1][j] then begin
		result:=true;
		exit;
	end else
	if (not w[i+1][j]) and (i<n) then  begin
		b[i+1][j]:=dfs(i+1,j);
		if b[i+1][j] then begin
			result:=true;
			exit;
		end;
	end;
end;
if a[i][j-1] = a[i][j] then begin
	if w[i][j-1] and b[i][j-1] then begin
		result:=true;
		exit;
	end else
	if (not w[i][j-1]) and (j>1) then  begin
		b[i][j-1]:=dfs(i,j-1);
		if b[i][j-1] then begin
			result:=true;
			exit;
		end;
	end;
end;
if a[i][j+1] = a[i][j] then begin
	if w[i][j+1] and b[i][j+1] then begin
		result:=true;
		exit;
	end else
	if (not w[i][j+1]) and (j<m) then   begin
		b[i][j+1]:=dfs(i,j+1);
		if b[i][j+1] then begin
			result:=true;
			exit;
		end;
	end;
end;
end;
 
procedure bfs(i,j:longint);
 
var
	h,t,x,y:longint;
	q:array[0..10010,1..2] of longint;
 
	procedure put(i,j:longint);
	begin
	b[i][j]:=true;
	inc(t);
	q[t][1]:=i;
	q[t][2]:=j;
	end;
 
begin
b[i][j]:=true;
h:=1;
t:=1;
q[1][1]:=i;
q[1][2]:=j;
while h<=t do begin
	x:=q[h][1];
	y:=q[h][2];
	if (not b[x-1][y]) and (x>1) then put(x-1,y);
	if (not b[x+1][y]) and (x<n) then put(x+1,y);
	if (not b[x][y-1]) and (y>1) then put(x,y-1);
	if (not b[x][y+1]) and (y<m) then put(x,y+1);
	inc(h);
end;
end;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n,m);
fillchar(b,sizeof(b),0);
fillchar(w,sizeof(w),0);
for i:=0 to 110 do
	for j:=0 to 110 do
		a[i][j]:=maxlongint;
for i:=1 to n do
	for j:=1 to m do
		read(a[i][j]);
for i:=1 to n do
	for j:=1 to m do
		if not w[i][j] then b[i][j]:=dfs(i,j);
ans:=0; 
for i:=1 to n do
	for j:=1 to m do
		if not b[i][j] then begin
			bfs(i,j);
			inc(ans);
		end;
write(ans);
end.